package com.yj.generator.utils;

import com.yj.generator.constants.ConfigConstant;
import com.yj.generator.core.GenerateFileProxy;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.*;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;


/**
 * @author DengLin
 * @title 配置读取工具类
 * @date 2024/07/04
 */
public class PropertiesUtil {

    private static final Logger log = LogManager.getLogger(PropertiesUtil.class);

    private static final Map<String, Object> propertiesMap = new ConcurrentHashMap<>();

    // 加载配置
    static {
        Properties properties = new Properties();
        InputStreamReader inputStreamReader = null;
        try {
            inputStreamReader = new InputStreamReader(new FileInputStream(ConfigConstant.CONFIG_PROPERTIES_PATH));
            properties.load(inputStreamReader);
            Set<String> propertyNames = properties.stringPropertyNames();
            for (String propertyName : propertyNames) {
                propertiesMap.put(propertyName, properties.get(propertyName));
            }
        } catch (IOException e) {
            log.error("读取配置文件异常:{}", e.getMessage());
            throw new RuntimeException(e);
        } finally {
            if (inputStreamReader != null) {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    log.error("文件关闭异常:{}", e.getMessage());
                    throw new RuntimeException(e);
                }
            }
        }
    }

    private PropertiesUtil() {

    }

    public static String getProperty(String key) {
        return (String) propertiesMap.get(key);
    }

    public static void main(String[] args) {
        System.out.println(getProperty("datasource.url"));
    }


}
